﻿Imports System.Configuration

Public Class tConfiguration

#Region " Public Constants "
    Public Const CONFIG_SECTION_AUTOSAVE As String = "terrariaSvcWrapper/terrariaAutosave"
    Public Const CONFIG_SECTION_SERVERS As String = "terrariaSvcWrapper/terrariaServers"
#End Region

#Region " Private Members "
    Private pServers As TerrariaServersConfigurationElementCollection
    Private pAutoSave As TerrariaAutosaveConfigurationSection
#End Region

#Region " Public Properties "

    Public Property Servers() As TerrariaServersConfigurationElementCollection
        Get
            Return pServers
        End Get
        Set(ByVal value As TerrariaServersConfigurationElementCollection)
            pServers = value
        End Set
    End Property

    Public Property AutoSave() As TerrariaAutosaveConfigurationSection
        Get
            Return pAutoSave
        End Get
        Set(ByVal value As TerrariaAutosaveConfigurationSection)
            pAutoSave = value
        End Set
    End Property

    Public Sub ReadFromConfig()
        pServers = CType(ConfigurationManager.GetSection(CONFIG_SECTION_SERVERS), TerrariaServersConfigurationSection).Servers
        pAutoSave = CType(ConfigurationManager.GetSection(CONFIG_SECTION_AUTOSAVE), TerrariaAutosaveConfigurationSection)
    End Sub

#End Region
End Class


Public Class TerrariaAutosaveConfigurationSection
    Inherits ConfigurationSection

    <ConfigurationProperty("enabled", DefaultValue:="false", IsRequired:=False)> _
    Public Property Enabled() As Boolean
        Get
            Return CType(Me("enabled"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("enabled") = value
        End Set
    End Property

    <ConfigurationProperty("interval", DefaultValue:="60", IsRequired:=False)> _
    Public Property Interval() As Int32
        Get
            Return CType(Me("interval"), Int32)
        End Get
        Set(ByVal value As Int32)
            Me("interval") = value
        End Set
    End Property

    ''' <summary>
    ''' Returns the interval converted from minutes to milliseconds
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property IntervalInMilliseconds() As Int32
        Get
            Return (Interval * 60 * 1000)
        End Get
    End Property

End Class

Public Class TerrariaServersConfigurationSection
    Inherits ConfigurationSection

    <ConfigurationProperty("servers", IsDefaultCollection:=True)> _
    Public ReadOnly Property Servers() As TerrariaServersConfigurationElementCollection
        Get
            Return CType(Me("servers"), TerrariaServersConfigurationElementCollection)
        End Get
    End Property

End Class

Public Class TerrariaServersConfigurationElementCollection
    Inherits ConfigurationElementCollection


    Public Overrides ReadOnly Property CollectionType() As ConfigurationElementCollectionType
        Get
            Return ConfigurationElementCollectionType.AddRemoveClearMap
        End Get
    End Property

    Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
        Return New TerrariaServerConfigurationElement()
    End Function

    Protected Overloads Overrides Function CreateNewElement(ByVal elementName As String) As ConfigurationElement
        Return New TerrariaServerConfigurationElement(elementName)
    End Function

    Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As [Object]
        Return CType(element, TerrariaServerConfigurationElement).Name
    End Function

    Public Shadows Property AddElementName() As String
        Get
            Return MyBase.AddElementName
        End Get

        Set(ByVal value As String)
            MyBase.AddElementName = value
        End Set
    End Property

    Public Shadows Property ClearElementName() As String
        Get
            Return MyBase.ClearElementName
        End Get

        Set(ByVal value As String)
            MyBase.ClearElementName = value
        End Set
    End Property

    Public Shadows ReadOnly Property RemoveElementName() As String
        Get
            Return MyBase.RemoveElementName
        End Get
    End Property

    Public Shadows ReadOnly Property Count() As Integer
        Get
            Return MyBase.Count
        End Get
    End Property

    Default Public Shadows Property Item(ByVal index As Integer) As TerrariaServerConfigurationElement
        Get
            Return CType(BaseGet(index), TerrariaServerConfigurationElement)
        End Get
        Set(ByVal value As TerrariaServerConfigurationElement)
            If Not (BaseGet(index) Is Nothing) Then
                BaseRemoveAt(index)
            End If
            BaseAdd(index, value)
        End Set
    End Property

    Default Public Shadows ReadOnly Property Item(ByVal Name As String) As TerrariaServerConfigurationElement
        Get
            Return CType(BaseGet(Name), TerrariaServerConfigurationElement)
        End Get
    End Property

    Public Function IndexOf(ByVal terrariaServer As TerrariaServerConfigurationElement) As Integer
        Return BaseIndexOf(terrariaServer)
    End Function

    Public Sub Add(ByVal terrariaServer As TerrariaServerConfigurationElement)
        BaseAdd(terrariaServer)
        ' Add custom code here. 
    End Sub

    Protected Overrides Sub BaseAdd(ByVal element As ConfigurationElement)
        BaseAdd(element, False)
        ' Add custom code here. 
    End Sub 'BaseAdd

    Public Overloads Sub Remove(ByVal terrariaServer As TerrariaServerConfigurationElement)
        If BaseIndexOf(terrariaServer) >= 0 Then
            BaseRemove(terrariaServer.Name)
        End If
    End Sub

    Public Sub RemoveAt(ByVal index As Integer)
        BaseRemoveAt(index)
    End Sub

    Public Overloads Sub Remove(ByVal name As String)
        BaseRemove(name)
    End Sub

    Public Sub Clear()
        BaseClear()
    End Sub



End Class


Public Class TerrariaServerConfigurationElement
    Inherits ConfigurationElement
    Private pBufferConsole As Boolean = False
    Private pBufferErrors As Boolean = False

    'default constructor
    Public Sub New()

    End Sub

    Public Sub New(ByVal elementName As String)
        Name = elementName
    End Sub

    Public Property BufferErrors() As Boolean
        Get
            Return pBufferErrors
        End Get
        Set(ByVal value As Boolean)
            pBufferErrors = value
        End Set
    End Property


    Public Property BufferConsole() As Boolean
        Get
            Return pBufferConsole
        End Get
        Set(ByVal value As Boolean)
            pBufferConsole = value
        End Set
    End Property


    <ConfigurationProperty("name", DefaultValue:="", IsRequired:=True)> _
    Public Property Name() As String
        Get
            Return CType(Me("name"), String)
        End Get
        Set(ByVal value As String)
            Me("name") = value
        End Set
    End Property

    <ConfigurationProperty("location", DefaultValue:="", IsRequired:=True)> _
    Public Property Location() As String
        Get
            Return CType(Me("location"), String)
        End Get
        Set(ByVal value As String)
            Me("location") = value
        End Set
    End Property

    <ConfigurationProperty("arguments", DefaultValue:="", IsRequired:=False)> _
    Public Property Arguments() As String
        Get
            Return CType(Me("arguments"), String)
        End Get
        Set(ByVal value As String)
            Me("arguments") = value
        End Set
    End Property

    <ConfigurationProperty("allowAutoSave", DefaultValue:="true", IsRequired:=False)> _
    Public Property AllowAutoSave() As Boolean
        Get
            Return CType(Me("allowAutoSave"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("allowAutoSave") = value
        End Set
    End Property

    <ConfigurationProperty("logErrors", DefaultValue:="false", IsRequired:=False)> _
    Public Property LogErrors() As Boolean
        Get
            Return CType(Me("logErrors"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("logErrors") = value
        End Set
    End Property

    <ConfigurationProperty("logConsoleOutput", DefaultValue:="false", IsRequired:=False)> _
    Public Property LogConsoleOutput() As Boolean
        Get
            Return CType(Me("logConsoleOutput"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("logConsoleOutput") = value
        End Set
    End Property

    <ConfigurationProperty("shutdownCommand", DefaultValue:="false", IsRequired:=True)> _
    Public Property ShutdownCommand() As String
        Get
            Return CType(Me("shutdownCommand"), String)
        End Get
        Set(ByVal value As String)
            Me("shutdownCommand") = value
        End Set
    End Property

    <ConfigurationProperty("broadcastCommand", DefaultValue:="false", IsRequired:=True)> _
    Public Property BroadcastCommand() As String
        Get
            Return CType(Me("broadcastCommand"), String)
        End Get
        Set(ByVal value As String)
            Me("broadcastCommand") = value
        End Set
    End Property

    <ConfigurationProperty("saveCommand", DefaultValue:="false", IsRequired:=True)> _
    Public Property SaveCommand() As String
        Get
            Return CType(Me("saveCommand"), String)
        End Get
        Set(ByVal value As String)
            Me("saveCommand") = value
        End Set
    End Property

    <ConfigurationProperty("tShockWorld", DefaultValue:="", IsRequired:=False)> _
    Public Property TShockWorld() As String
        Get
            Return CType(Me("tShockWorld"), String)
        End Get
        Set(ByVal value As String)
            Me("tShockWorld") = value
        End Set
    End Property

    <ConfigurationProperty("tShockPort", DefaultValue:="", IsRequired:=False)> _
    Public Property TShockPort() As String
        Get
            Return CType(Me("tShockPort"), String)
        End Get
        Set(ByVal value As String)
            Me("tShockPort") = value
        End Set
    End Property



    '********************** TUTORIAL CODE
    Protected Overrides Sub DeserializeElement(ByVal reader As System.Xml.XmlReader, ByVal serializeCollectionKey As Boolean)
        MyBase.DeserializeElement(reader, serializeCollectionKey)
        ' Enter your custom processing code here. 
    End Sub

    Protected Overrides Function SerializeElement(ByVal writer As System.Xml.XmlWriter, ByVal serializeCollectionKey As Boolean) As Boolean

        Dim ret As Boolean = MyBase.SerializeElement(writer, serializeCollectionKey)
        ' Enter your custom processing code here. 
        Return ret
    End Function 'SerializeElement

    Protected Overrides Function IsModified() As Boolean
        Dim ret As Boolean = MyBase.IsModified()
        ' Enter your custom processing code here. 
        Return ret
    End Function 'IsModified





End Class

